require(['grid', 'topBar', 'layout'], function () {
    var maintenaPlanEditDialog,
        partsReplaceEditDialog,
        $maintenaPlanList = $('#maintenaPlanList'),
        $partsReplaceSetInfoList = $('#partsReplaceSetInfoList');
    // 弹出层控制-新增维保记录
    $("#addMaintenaPlan").click(function () {
        getMaintenaPlanEditDialog({
            title: '新增维保计划'
        });
    });
    //弹出层控制-新增零部件计划
    $("#addSparePartPlan").click(function () {
        getPartsReplaceEditDialog({
            title: '新增零部件计划'
        });
    });

    $('.table-wrap').height($(window).height() - 81);
    $(window).on('resize', function () {
        $('.table-wrap').height($(window).height() - 81);
        $maintenaPlanList.grid('resize');
        $partsReplaceSetInfoList.grid('resize');
    });

    //初始化维保计划表格数据
    $maintenaPlanList.grid({
        url: "/funMaintenaSet/getMaintenRmndrSetInfoList.htm",
        datatype: "JSON",
        colModel: [
            {name: "rcrdId", index: "rcrdId", label: 'rcrdId', key: true, hidden: true},
            {name: "carType", index: "carType", label: '车型', width: 100},
            {name: "mntType", index: "mntType", label: '维保类型'},
            {name: "itrvMile", index: "itrvMile", label: '保养里程（km）', width: 100},
            {name: "itrvTime", index: "itrvTime", label: '间隔时间（天）', width: 100},
            {
                name: "operate", width: 130, label: '操作', title: false,
                formatter: 'operArea',
                formatoptions: [{
                    text: '编辑',
                    css: 'operate-edit',
                    click: function (data) {
                        getMaintenaPlanEditDialog({'title': '编辑维保计划'}).dialog('exec', 'setVal', data.rcrdId);
                    }
                }, {
                    text: '删除',
                    css: 'operate-delete',
                    click: function (data) {
                        $.messager.confirm('删除', '确定要删除该条记录吗？', 'delete', function (r) {
                            if (r) {
                                $.ajaxExt({
                                    url: "/funMaintenaSet/delMaintenRmndrSetInfo.htm",
                                    data: {'rcrdId': data.rcrdId}
                                }).done(function (data) {
                                    reloadMaintenaPlanGridData();
                                });
                            }
                        });
                    }
                }]

            }
        ],
        multiselect: false
    });
    //初始化零部件计划表格数据
    $partsReplaceSetInfoList.grid({
        url: "/funMaintenaSet/getPartsReplaceSetInfoList.htm",
        datatype: "JSON",
        colModel: [
            {name: "rcrdId", index: "rcrdId", label: 'rcrd_id', key: true, hidden: true},
            {name: "carType", index: "carType", label: '车型', width: 100, hidden: true},
            {name: "carTypeName", index: "carTypeName", label: '车型', width: 100},
            {name: "mntMile", index: "mntMile", label: '保养里程（km）'},
            {name: "partsCode", index: "partsCode", label: '建议更换零部件', width: 100},
            {
                name: "operate", width: 130, label: '操作', title: false,
                formatter: 'operArea',
                formatoptions: [{
                    text: '编辑',
                    css: 'operate-edit',
                    click: function (data) {
                        getPartsReplaceEditDialog({'title': '编辑零件计划'}).dialog('exec', 'setVal', data.carType, data.mntMile);
                    }
                }, {
                    text: '删除',
                    css: 'operate-delete',
                    click: function (data) {
                        $.messager.confirm('删除', '确定要删除该条记录吗？', 'delete', function (r) {
                            if (r) {
                                $.ajaxExt({
                                    url: "/funMaintenaSet/delPartsReplaceSetInfo.htm",
                                    data: {'carType': data.carType, 'mntMile': data.mntMile}
                                }).done(function (data) {
                                    $.messager.alert("提示", "成功删除一条记录", "success");
                                    reloadPartsReplaceSetGridData();
                                });
                            }
                        });
                    }
                }]

            }
        ],
        multiselect: false
    });


    /**
     * 获取维保计划对话框
     *
     * @param option
     *  {object} 对话框配置
     * @returns {jQuery}
     */
    function getMaintenaPlanEditDialog(option) {
        option = option || {};
        if (maintenaPlanEditDialog) {
            maintenaPlanEditDialog.dialog('open');
            if (option.title) {
                maintenaPlanEditDialog.dialog('setOption', 'title', option.title);
            }
            return maintenaPlanEditDialog;
        } else {
            maintenaPlanEditDialog = $('<div>').dialog(
                {
                    title: option.title || '设置维保计划',
                    url: './mainte/setting/funMaintenaInputSet.htm',
                    height: 390,
                    width: 700,
                    autoOpen: true,
                    close: function () {
                        $(this).dialog('exec', 'reset');
                    },
                    buttons: [
                        {
                            text: '保存',
                            css: 'mr10',
                            click: function () {
                                var _t = this;
                                $(_t).dialog('exec', 'add', function () {
                                    $(_t).dialog('close');
                                    reloadMaintenaPlanGridData();
                                });
                            }
                        }, {
                            text: '取消',
                            css: 'btn-cancel',
                            click: function () {
                                $(this).dialog('close');
                            }
                        }]
                });
            return maintenaPlanEditDialog;
        }
    }

    /**
     * 获取零部件计划对话框
     *
     * @param option
     *  {object} 对话框配置
     * @returns {jQuery}
     */
    function getPartsReplaceEditDialog(option) {
        option = option || {};
        if (partsReplaceEditDialog) {
            partsReplaceEditDialog.dialog('open');
            if (option.title) {
                partsReplaceEditDialog.dialog('setOption', 'title', option.title);
            }
            return partsReplaceEditDialog;
        } else {
            partsReplaceEditDialog = $('<div>').dialog(
                {
                    title: option.title,
                    url: './mainte/setting/funSparePartsSet.htm',
                    height: 400,
                    width: 850,
                    autoOpen: true,
                    close: function () {
                        $(this).dialog('exec', 'reset');
                    },
                    buttons: [
                        {
                            text: '保存',
                            css: 'mr10',
                            click: function () {
                                var _t = this;
                                $(_t).dialog('exec', 'add', function () {
                                    $(_t).dialog('close');
                                    reloadPartsReplaceSetGridData();
                                });
                            }
                        }, {
                            text: '取消',
                            css: 'btn-cancel',
                            click: function () {
                                $(this).dialog('close');
                            }
                        }]
                });
            return partsReplaceEditDialog;
        }
    }

    //重新查询维保计划grid
    var reloadMaintenaPlanGridData = function () {
        $maintenaPlanList.grid('setGridParam', {
            datatype: "JSON"
        }).trigger("reloadGrid");
    };
    //重新查询零部件grid
    var reloadPartsReplaceSetGridData = function () {
        $partsReplaceSetInfoList.grid('setGridParam', {
            datatype: "JSON"
        }).trigger("reloadGrid");
    };
});